package com.ljd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljd.client.UcenterClient;
import com.ljd.entity.StatisticsDaily;
import com.ljd.mapper.StatisticsDailyMapper;
import com.ljd.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljd.util.R;
import com.why.handler.GuliException;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2022-01-07
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    UcenterClient client;
    @Override
    public void createStatisticsByDay(String day) {
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated,day);
        Integer integer = baseMapper.selectCount(wrapper);
        if(integer>=0){
            baseMapper.delete(wrapper);
        }

        R registerCount = client.getRegisterCount(day);
        Integer count = (Integer) registerCount.getData().get("count");
        StatisticsDaily daily = new StatisticsDaily();
        daily.setDateCalculated(day);
        daily.setRegisterNum(count);
        daily.setLoginNum(RandomUtils.nextInt(100,200));
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));
        daily.setCourseNum(RandomUtils.nextInt(100,200));
        int insert = baseMapper.insert(daily);
        if(insert<= 0){
            throw new GuliException(20001,"添加注册记录失败");
        }
    }

    @Override
    public Map<String, Object> showChart(String type, String start, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select("date_calculated",type)
                .between("date_calculated",start,end);
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);
        List<String> dateList = new ArrayList<>(); // 日期 x
        List<Integer> dataList = new ArrayList<>(); // 数据 y

        for (StatisticsDaily daily : dailyList) {
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case  "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("dataList",dataList);
        map.put("dateList",dateList);
        return map;
    }
}
